package com.yuwei.user

import android.Manifest
import android.content.pm.PackageManager
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.yuwei.base.RoutePath
import com.yuwei.common.mvi.BaseViewActivity
import com.yuwei.user.databinding.ActivityPhoneBookBinding

@Route(path = RoutePath.MODULE_PHONE_BOOK_ACTIVITY)
class PhoneBookActivity : BaseViewActivity<ActivityPhoneBookBinding>() {

    override fun initView() {
        binding.tvJump.setOnClickListener {
            ARouter.getInstance().build(RoutePath.MAIN_ACTIVITY).navigation()
        }
        binding.btnPhoneBook.setOnClickListener {
            requestContactsPermission()
        }
    }

    override fun initData() {

    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 100) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "通讯录权限已开启", Toast.LENGTH_SHORT).show()
            }
            ARouter.getInstance().build(RoutePath.MAIN_ACTIVITY).navigation()
        }
    }
    private fun requestContactsPermission() {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.READ_CONTACTS),
            100
        )
    }
}